using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;
namespace SubSonicResearch
{
	/// <summary>
	/// Strongly-typed collection for the Message class.
	/// </summary>
    [Serializable]
	public partial class MessageCollection : ActiveList<Message, MessageCollection>
	{	   
		public MessageCollection() {}
        
        /// <summary>
		/// Filters an existing collection based on the set criteria. This is an in-memory filter
		/// Thanks to developingchris for this!
        /// </summary>
        /// <returns>MessageCollection</returns>
		public MessageCollection Filter()
        {
            for (int i = this.Count - 1; i > -1; i--)
            {
                Message o = this[i];
                foreach (SubSonic.Where w in this.wheres)
                {
                    bool remove = false;
                    System.Reflection.PropertyInfo pi = o.GetType().GetProperty(w.ColumnName);
                    if (pi.CanRead)
                    {
                        object val = pi.GetValue(o, null);
                        switch (w.Comparison)
                        {
                            case SubSonic.Comparison.Equals:
                                if (!val.Equals(w.ParameterValue))
                                {
                                    remove = true;
                                }
                                break;
                        }
                    }
                    if (remove)
                    {
                        this.Remove(o);
                        break;
                    }
                }
            }
            return this;
        }
		
		
	}
	/// <summary>
	/// This is an ActiveRecord class which wraps the Messages table.
	/// </summary>
	[Serializable]
	public partial class Message : ActiveRecord<Message>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public Message()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}
		
		private void InitSetDefaults() { SetDefaults(); }
		
		public Message(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}
        
		public Message(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}
		 
		public Message(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}
		
		protected static void SetSQLProps() { GetTableSchema(); }
		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}
		}
		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Messages", TableType.Table, DataService.GetInstance("ssDSN"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarMessageId = new TableSchema.TableColumn(schema);
				colvarMessageId.ColumnName = "MessageId";
				colvarMessageId.DataType = DbType.Int32;
				colvarMessageId.MaxLength = 0;
				colvarMessageId.AutoIncrement = true;
				colvarMessageId.IsNullable = false;
				colvarMessageId.IsPrimaryKey = true;
				colvarMessageId.IsForeignKey = false;
				colvarMessageId.IsReadOnly = false;
				colvarMessageId.DefaultSetting = @"";
				colvarMessageId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarMessageId);
				
				TableSchema.TableColumn colvarSenderUserId = new TableSchema.TableColumn(schema);
				colvarSenderUserId.ColumnName = "SenderUserId";
				colvarSenderUserId.DataType = DbType.Int32;
				colvarSenderUserId.MaxLength = 0;
				colvarSenderUserId.AutoIncrement = false;
				colvarSenderUserId.IsNullable = false;
				colvarSenderUserId.IsPrimaryKey = false;
				colvarSenderUserId.IsForeignKey = true;
				colvarSenderUserId.IsReadOnly = false;
				colvarSenderUserId.DefaultSetting = @"";
				
					colvarSenderUserId.ForeignKeyTableName = "People";
				schema.Columns.Add(colvarSenderUserId);
				
				TableSchema.TableColumn colvarSubject = new TableSchema.TableColumn(schema);
				colvarSubject.ColumnName = "Subject";
				colvarSubject.DataType = DbType.AnsiString;
				colvarSubject.MaxLength = 255;
				colvarSubject.AutoIncrement = false;
				colvarSubject.IsNullable = false;
				colvarSubject.IsPrimaryKey = false;
				colvarSubject.IsForeignKey = false;
				colvarSubject.IsReadOnly = false;
				colvarSubject.DefaultSetting = @"";
				colvarSubject.ForeignKeyTableName = "";
				schema.Columns.Add(colvarSubject);
				
				TableSchema.TableColumn colvarBody = new TableSchema.TableColumn(schema);
				colvarBody.ColumnName = "Body";
				colvarBody.DataType = DbType.AnsiString;
				colvarBody.MaxLength = 2147483647;
				colvarBody.AutoIncrement = false;
				colvarBody.IsNullable = false;
				colvarBody.IsPrimaryKey = false;
				colvarBody.IsForeignKey = false;
				colvarBody.IsReadOnly = false;
				colvarBody.DefaultSetting = @"";
				colvarBody.ForeignKeyTableName = "";
				schema.Columns.Add(colvarBody);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["ssDSN"].AddSchema("Messages",schema);
			}
		}
		#endregion
		
		#region Props
		  
		[XmlAttribute("MessageId")]
		[Bindable(true)]
		public int MessageId 
		{
			get { return GetColumnValue<int>(Columns.MessageId); }
			set { SetColumnValue(Columns.MessageId, value); }
		}
		  
		[XmlAttribute("SenderUserId")]
		[Bindable(true)]
		public int SenderUserId 
		{
			get { return GetColumnValue<int>(Columns.SenderUserId); }
			set { SetColumnValue(Columns.SenderUserId, value); }
		}
		  
		[XmlAttribute("Subject")]
		[Bindable(true)]
		public string Subject 
		{
			get { return GetColumnValue<string>(Columns.Subject); }
			set { SetColumnValue(Columns.Subject, value); }
		}
		  
		[XmlAttribute("Body")]
		[Bindable(true)]
		public string Body 
		{
			get { return GetColumnValue<string>(Columns.Body); }
			set { SetColumnValue(Columns.Body, value); }
		}
		
		#endregion
		
		
		#region PrimaryKey Methods		
		
        protected override void SetPrimaryKey(object oValue)
        {
            base.SetPrimaryKey(oValue);
            
            SetPKValues();
        }
        
		
		public SubSonicResearch.RecipientCollection Recipients()
		{
			return new SubSonicResearch.RecipientCollection().Where(Recipient.Columns.MessageId, MessageId).Load();
		}
		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a Person ActiveRecord object related to this Message
		/// 
		/// </summary>
		public SubSonicResearch.Person Person
		{
			get { return SubSonicResearch.Person.FetchByID(this.SenderUserId); }
			set { SetColumnValue("SenderUserId", value.PersonId); }
		}
		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(int varSenderUserId,string varSubject,string varBody)
		{
			Message item = new Message();
			
			item.SenderUserId = varSenderUserId;
			
			item.Subject = varSubject;
			
			item.Body = varBody;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varMessageId,int varSenderUserId,string varSubject,string varBody)
		{
			Message item = new Message();
			
				item.MessageId = varMessageId;
			
				item.SenderUserId = varSenderUserId;
			
				item.Subject = varSubject;
			
				item.Body = varBody;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn MessageIdColumn
        {
            get { return Schema.Columns[0]; }
        }
        
        
        
        public static TableSchema.TableColumn SenderUserIdColumn
        {
            get { return Schema.Columns[1]; }
        }
        
        
        
        public static TableSchema.TableColumn SubjectColumn
        {
            get { return Schema.Columns[2]; }
        }
        
        
        
        public static TableSchema.TableColumn BodyColumn
        {
            get { return Schema.Columns[3]; }
        }
        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string MessageId = @"MessageId";
			 public static string SenderUserId = @"SenderUserId";
			 public static string Subject = @"Subject";
			 public static string Body = @"Body";
						
		}
		#endregion
		
		#region Update PK Collections
		
        public void SetPKValues()
        {
}
        #endregion
    
        #region Deep Save
		
        public void DeepSave()
        {
            Save();
            
}
        #endregion
	}
}
